﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

namespace INFTUR
{
    public partial class VenderPacote : System.Web.UI.Page
    {
        public static bool controle = false;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void limpar()
        {
            tbCodigo.Text = "";
            tbCodigo.Enabled = false;
            tbData.Text = "";
            tbData.Enabled = false;
            tbForma.Text = "";
            tbForma.Enabled = false;
            dlCliente.SelectedValue = "0";
            dlCliente.Enabled = false;
            dlPacote.SelectedValue = "0";
            dlPacote.Enabled = false;
            btGravar.Enabled = false;
            btCancelar.Enabled = false;
            

        }

        protected void btGravar_Click(object sender, EventArgs e)
        {
            try
            {
                Session["usuario"] = "brasil";
                VendaPacoteBO vendaBO = new VendaPacoteBO();

                ClienteBO cBo = new ClienteBO();
                cBo.Cpf = Convert.ToString(dlCliente.SelectedValue);
                vendaBO.Cliente = cBo;
                PacoteBO pBO = new PacoteBO();
                pBO.Id = Convert.ToInt32(dlPacote.SelectedValue);
                vendaBO.Pacote = pBO;
                vendaBO.Data = Convert.ToDateTime(tbData.Text);
                vendaBO.Pagamento = tbForma.Text;
                UsuarioBO usuBO = new UsuarioBO();
                usuBO.Login = Session["usuario"].ToString();
                vendaBO.Usuario = usuBO;
                VendaPacoteBOMetodos vendaBOMetodos = new VendaPacoteBOMetodos();

                if (controle == true)
                {
                    vendaBOMetodos.inserir(vendaBO);
                    GridView1.DataBind();
                    limpar();


                }
                else
                {
                    vendaBO.Id = Convert.ToInt32(tbCodigo.Text);
                    vendaBOMetodos.atualizar(vendaBO);
                    GridView1.DataBind();
                    limpar();
                }
            }
            catch (Exception ex)
            {
                lbErro.Text = ex.Message;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            tbCodigo.Enabled = false;
            tbData.Enabled = true;
            tbForma.Enabled = true;
            dlCliente.Enabled = true;
            dlPacote.Enabled = true;
            btGravar.Enabled = true;
            btCancelar.Enabled = true;
            controle = true;
        }

        protected void btCancelar_Click(object sender, EventArgs e)
        {
            limpar();
        }

        protected void lbAtualiza_Click(object sender, EventArgs e)
        {
            controle = false;
            LinkButton lb = ((LinkButton)sender);
            DataControlFieldCell celula = (DataControlFieldCell)lb.Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;

            int id = Convert.ToInt32(linha.Cells[0].Text);

            VendaPacoteBOMetodos vendaMetodos = new VendaPacoteBOMetodos();

            VendaPacoteBO venda = new VendaPacoteBO();

            venda = vendaMetodos.consultar(id);
            tbCodigo.Text = Convert.ToString(venda.Id);
            tbData.Text = Convert.ToString(venda.Data);
            tbForma.Text = Convert.ToString(venda.Pagamento);
            dlPacote.SelectedValue = Convert.ToString(venda.Pacote.Id);
            dlCliente.SelectedValue = Convert.ToString(venda.Cliente.Cpf);

            tbCodigo.Enabled = false;
            tbData.Enabled = true;
            tbForma.Enabled = true;
            dlCliente.Enabled = true;
            dlPacote.Enabled = true;
            btGravar.Enabled = true;
            btCancelar.Enabled = true;
        }

        protected void lbRemove_Click(object sender, EventArgs e)
        {
            LinkButton lb = ((LinkButton)sender);
            DataControlFieldCell celula = (DataControlFieldCell)lb.Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;

            int id = Convert.ToInt32(linha.Cells[0].Text);

            VendaPacoteBOMetodos vendaPacote = new VendaPacoteBOMetodos();

            VendaPacoteBO venda = new VendaPacoteBO();

            venda = vendaPacote.consultar(id);

            vendaPacote.remover(venda);
            GridView1.DataBind();
            limpar();
        }

       
    }
}
